{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.076945900Z",
     "start_time": "2025-06-06T02:43:37.038519700Z"
    }
   },
   "outputs": [],
   "source": [
    "import  pandas as pd\n",
    "import numpy as np\n",
    "import sys\n",
    "\n",
    "import os\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import datetime\n",
    "# from utils.log import Logger\n",
    "# from utils.common import data_preprocessing\n",
    "from xgboost import XGBRegressor\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.metrics import mean_squared_error, mean_absolute_error, root_mean_squared_error\n",
    "import joblib\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "data=pd.read_csv('../../data/raw/train.csv')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.077946800Z",
     "start_time": "2025-06-06T02:43:37.046385700Z"
    }
   },
   "id": "989e26d1a1039a1b"
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [],
   "source": [
    "ana_data=data.copy()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.131598200Z",
     "start_time": "2025-06-06T02:43:37.069434100Z"
    }
   },
   "id": "bb0cfc149898202e"
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1100 entries, 0 to 1099\n",
      "Data columns (total 31 columns):\n",
      " #   Column                    Non-Null Count  Dtype \n",
      "---  ------                    --------------  ----- \n",
      " 0   Attrition                 1100 non-null   int64 \n",
      " 1   Age                       1100 non-null   int64 \n",
      " 2   BusinessTravel            1100 non-null   object\n",
      " 3   Department                1100 non-null   object\n",
      " 4   DistanceFromHome          1100 non-null   int64 \n",
      " 5   Education                 1100 non-null   int64 \n",
      " 6   EducationField            1100 non-null   object\n",
      " 7   EmployeeNumber            1100 non-null   int64 \n",
      " 8   EnvironmentSatisfaction   1100 non-null   int64 \n",
      " 9   Gender                    1100 non-null   object\n",
      " 10  JobInvolvement            1100 non-null   int64 \n",
      " 11  JobLevel                  1100 non-null   int64 \n",
      " 12  JobRole                   1100 non-null   object\n",
      " 13  JobSatisfaction           1100 non-null   int64 \n",
      " 14  MaritalStatus             1100 non-null   object\n",
      " 15  MonthlyIncome             1100 non-null   int64 \n",
      " 16  NumCompaniesWorked        1100 non-null   int64 \n",
      " 17  Over18                    1100 non-null   object\n",
      " 18  OverTime                  1100 non-null   object\n",
      " 19  PercentSalaryHike         1100 non-null   int64 \n",
      " 20  PerformanceRating         1100 non-null   int64 \n",
      " 21  RelationshipSatisfaction  1100 non-null   int64 \n",
      " 22  StandardHours             1100 non-null   int64 \n",
      " 23  StockOptionLevel          1100 non-null   int64 \n",
      " 24  TotalWorkingYears         1100 non-null   int64 \n",
      " 25  TrainingTimesLastYear     1100 non-null   int64 \n",
      " 26  WorkLifeBalance           1100 non-null   int64 \n",
      " 27  YearsAtCompany            1100 non-null   int64 \n",
      " 28  YearsInCurrentRole        1100 non-null   int64 \n",
      " 29  YearsSinceLastPromotion   1100 non-null   int64 \n",
      " 30  YearsWithCurrManager      1100 non-null   int64 \n",
      "dtypes: int64(23), object(8)\n",
      "memory usage: 266.5+ KB\n"
     ]
    }
   ],
   "source": [
    "ana_data.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.135001900Z",
     "start_time": "2025-06-06T02:43:37.075463200Z"
    }
   },
   "id": "f42a1946b1b31a98"
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "(1100, 31)"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ana_data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.135001900Z",
     "start_time": "2025-06-06T02:43:37.086945700Z"
    }
   },
   "id": "940fedf891bc3af7"
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "0"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看数据是否有缺失值  #无缺失值\n",
    "ana_data.isnull().sum().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.136035100Z",
     "start_time": "2025-06-06T02:43:37.092172Z"
    }
   },
   "id": "e7a20590963631ee"
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "Attrition\n0    922\n1    178\nName: count, dtype: int64"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value_counts = data['Attrition'].value_counts()\n",
    "value_counts"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.136035100Z",
     "start_time": "2025-06-06T02:43:37.101100Z"
    }
   },
   "id": "cf3447e4ce1a19d1"
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [],
   "source": [
    "# data1=data.groupby(['Attrition','Department','BusinessTravel','EducationField']).sum()\n",
    "# data1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.136035100Z",
     "start_time": "2025-06-06T02:43:37.108146600Z"
    }
   },
   "id": "17e331f7f460d9cf"
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [],
   "source": [
    "#热编码\n",
    "ana_data=pd.get_dummies(ana_data[['BusinessTravel', 'Department','EducationField','Gender','JobRole','MaritalStatus','Over18','OverTime']])\n",
    "# ana_data=pd.get_dummies(ana_data[['BusinessTravel', 'Department','EducationField','Gender','JobRole','MaritalStatus','Over18','OverTime']])\n",
    "# ana_data.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.137047600Z",
     "start_time": "2025-06-06T02:43:37.112434700Z"
    }
   },
   "id": "6ed82c58a54d5f30"
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1100 entries, 0 to 1099\n",
      "Data columns (total 29 columns):\n",
      " #   Column                             Non-Null Count  Dtype\n",
      "---  ------                             --------------  -----\n",
      " 0   BusinessTravel_Non-Travel          1100 non-null   bool \n",
      " 1   BusinessTravel_Travel_Frequently   1100 non-null   bool \n",
      " 2   BusinessTravel_Travel_Rarely       1100 non-null   bool \n",
      " 3   Department_Human Resources         1100 non-null   bool \n",
      " 4   Department_Research & Development  1100 non-null   bool \n",
      " 5   Department_Sales                   1100 non-null   bool \n",
      " 6   EducationField_Human Resources     1100 non-null   bool \n",
      " 7   EducationField_Life Sciences       1100 non-null   bool \n",
      " 8   EducationField_Marketing           1100 non-null   bool \n",
      " 9   EducationField_Medical             1100 non-null   bool \n",
      " 10  EducationField_Other               1100 non-null   bool \n",
      " 11  EducationField_Technical Degree    1100 non-null   bool \n",
      " 12  Gender_Female                      1100 non-null   bool \n",
      " 13  Gender_Male                        1100 non-null   bool \n",
      " 14  JobRole_Healthcare Representative  1100 non-null   bool \n",
      " 15  JobRole_Human Resources            1100 non-null   bool \n",
      " 16  JobRole_Laboratory Technician      1100 non-null   bool \n",
      " 17  JobRole_Manager                    1100 non-null   bool \n",
      " 18  JobRole_Manufacturing Director     1100 non-null   bool \n",
      " 19  JobRole_Research Director          1100 non-null   bool \n",
      " 20  JobRole_Research Scientist         1100 non-null   bool \n",
      " 21  JobRole_Sales Executive            1100 non-null   bool \n",
      " 22  JobRole_Sales Representative       1100 non-null   bool \n",
      " 23  MaritalStatus_Divorced             1100 non-null   bool \n",
      " 24  MaritalStatus_Married              1100 non-null   bool \n",
      " 25  MaritalStatus_Single               1100 non-null   bool \n",
      " 26  Over18_Y                           1100 non-null   bool \n",
      " 27  OverTime_No                        1100 non-null   bool \n",
      " 28  OverTime_Yes                       1100 non-null   bool \n",
      "dtypes: bool(29)\n",
      "memory usage: 31.3 KB\n"
     ]
    }
   ],
   "source": [
    "ana_data.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.154435700Z",
     "start_time": "2025-06-06T02:43:37.127998100Z"
    }
   },
   "id": "a74c0feff30a5530"
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "(1100, 29)"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ana_data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.156435400Z",
     "start_time": "2025-06-06T02:43:37.139055700Z"
    }
   },
   "id": "fbda5f74eaade4c"
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "   BusinessTravel_Non-Travel  BusinessTravel_Travel_Frequently  \\\n0                      False                             False   \n\n   BusinessTravel_Travel_Rarely  Department_Human Resources  \\\n0                          True                       False   \n\n   Department_Research & Development  Department_Sales  \\\n0                               True             False   \n\n   EducationField_Human Resources  EducationField_Life Sciences  \\\n0                           False                          True   \n\n   EducationField_Marketing  EducationField_Medical  ...  \\\n0                     False                   False  ...   \n\n   JobRole_Research Director  JobRole_Research Scientist  \\\n0                      False                       False   \n\n   JobRole_Sales Executive  JobRole_Sales Representative  \\\n0                    False                         False   \n\n   MaritalStatus_Divorced  MaritalStatus_Married  MaritalStatus_Single  \\\n0                    True                  False                 False   \n\n   Over18_Y  OverTime_No  OverTime_Yes  \n0      True         True         False  \n\n[1 rows x 29 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>BusinessTravel_Non-Travel</th>\n      <th>BusinessTravel_Travel_Frequently</th>\n      <th>BusinessTravel_Travel_Rarely</th>\n      <th>Department_Human Resources</th>\n      <th>Department_Research &amp; Development</th>\n      <th>Department_Sales</th>\n      <th>EducationField_Human Resources</th>\n      <th>EducationField_Life Sciences</th>\n      <th>EducationField_Marketing</th>\n      <th>EducationField_Medical</th>\n      <th>...</th>\n      <th>JobRole_Research Director</th>\n      <th>JobRole_Research Scientist</th>\n      <th>JobRole_Sales Executive</th>\n      <th>JobRole_Sales Representative</th>\n      <th>MaritalStatus_Divorced</th>\n      <th>MaritalStatus_Married</th>\n      <th>MaritalStatus_Single</th>\n      <th>Over18_Y</th>\n      <th>OverTime_No</th>\n      <th>OverTime_Yes</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>False</td>\n      <td>False</td>\n      <td>True</td>\n      <td>False</td>\n      <td>True</td>\n      <td>False</td>\n      <td>False</td>\n      <td>True</td>\n      <td>False</td>\n      <td>False</td>\n      <td>...</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>True</td>\n      <td>False</td>\n      <td>False</td>\n      <td>True</td>\n      <td>True</td>\n      <td>False</td>\n    </tr>\n  </tbody>\n</table>\n<p>1 rows × 29 columns</p>\n</div>"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ana_data.head(1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.262652Z",
     "start_time": "2025-06-06T02:43:37.147873500Z"
    }
   },
   "id": "e80b38d9479a5e9e"
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [],
   "source": [
    "data=pd.read_csv('../../data/raw/test2.csv')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.262652Z",
     "start_time": "2025-06-06T02:43:37.166237Z"
    }
   },
   "id": "ab454d90cf727640"
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 350 entries, 0 to 349\n",
      "Data columns (total 31 columns):\n",
      " #   Column                    Non-Null Count  Dtype \n",
      "---  ------                    --------------  ----- \n",
      " 0   Age                       350 non-null    int64 \n",
      " 1   BusinessTravel            350 non-null    object\n",
      " 2   Department                350 non-null    object\n",
      " 3   DistanceFromHome          350 non-null    int64 \n",
      " 4   Education                 350 non-null    int64 \n",
      " 5   EducationField            350 non-null    object\n",
      " 6   EmployeeNumber            350 non-null    int64 \n",
      " 7   EnvironmentSatisfaction   350 non-null    int64 \n",
      " 8   Gender                    350 non-null    object\n",
      " 9   JobInvolvement            350 non-null    int64 \n",
      " 10  JobLevel                  350 non-null    int64 \n",
      " 11  JobRole                   350 non-null    object\n",
      " 12  JobSatisfaction           350 non-null    int64 \n",
      " 13  MaritalStatus             350 non-null    object\n",
      " 14  MonthlyIncome             350 non-null    int64 \n",
      " 15  NumCompaniesWorked        350 non-null    int64 \n",
      " 16  Over18                    350 non-null    object\n",
      " 17  OverTime                  350 non-null    object\n",
      " 18  PercentSalaryHike         350 non-null    int64 \n",
      " 19  PerformanceRating         350 non-null    int64 \n",
      " 20  RelationshipSatisfaction  350 non-null    int64 \n",
      " 21  StandardHours             350 non-null    int64 \n",
      " 22  StockOptionLevel          350 non-null    int64 \n",
      " 23  TotalWorkingYears         350 non-null    int64 \n",
      " 24  TrainingTimesLastYear     350 non-null    int64 \n",
      " 25  WorkLifeBalance           350 non-null    int64 \n",
      " 26  YearsAtCompany            350 non-null    int64 \n",
      " 27  YearsInCurrentRole        350 non-null    int64 \n",
      " 28  YearsSinceLastPromotion   350 non-null    int64 \n",
      " 29  YearsWithCurrManager      350 non-null    int64 \n",
      " 30  Attrition                 350 non-null    int64 \n",
      "dtypes: int64(23), object(8)\n",
      "memory usage: 84.9+ KB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.263652Z",
     "start_time": "2025-06-06T02:43:37.175747600Z"
    }
   },
   "id": "c7b661428796e6a4"
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-06T02:43:37.263652Z",
     "start_time": "2025-06-06T02:43:37.185467Z"
    }
   },
   "id": "9863fb262248e1"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
